import mysql from 'mysql';
import fs from 'fs';

const env = process.env.NODE_ENV;
const config = JSON.parse(fs.readFileSync('config/config.json', 'utf8'))[env];
const sqlFile = 'base.sql';
const sql = fs.readFileSync(sqlFile, 'utf8');

const conn = mysql.createConnection({
  host: config.host,
  user: config.username,
  password: config.password,
  multipleStatements: true,
});

conn.connect();

const rebuild = async () => {
  await conn.query('drop database if exists ac');
  console.log('删除数据库成功');
  await conn.query('create database ac character set utf8');
  console.log('创建数据库成功');
  await conn.query('use ac');
  console.log('进入ac数据库');
  await conn.query(sql);
  console.log('build database');
  await conn.end();
};

rebuild().then(() => {
  console.log('done');
});
